package cn.yunhe.controller;

import cn.yunhe.entity.Permission;
import cn.yunhe.entity.Role;
import cn.yunhe.service.RoleService;
import cn.yunhe.util.UUIDGenerator;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: ssm_insurance
 * @description: juese
 * @author: 葛森
 * @create: 2018-09-15 16:50
 **/

@Controller
@RequestMapping("/role")
public class RoleController {


    @Resource
    private RoleService roleService;


    @RequestMapping("/test")
    public ModelAndView test(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("sider");
        return mv;
    }


    @RequestMapping("/getRole")
    public ModelAndView doftl(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("role");
        return mv;
    }
    @RequestMapping("/addRole")
    public ModelAndView ftldo(){
        return new ModelAndView("role_add");
    }
    @RequestMapping("/updatePermission")
    public ModelAndView ftlupdate(){
        return new ModelAndView("role_update");
    }

    @RequestMapping("/getAllRole")
    public ModelAndView getAllRole(int pageSize,int pageNum,Role role){
        Map<String,Object> map=new HashMap<>();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("role",role);
        ModelAndView mv=new ModelAndView();
        Page page=roleService.getAllRoleList(map);
        List<Role> list=page;

        int totlePages=page.getPages();

        System.out.println(totlePages);


        mv.setView(new MappingJackson2JsonView());
        Map map1=new HashMap();
        map1.put("page",page);
        map1.put("totlePages",totlePages);

        mv.addObject("map",map1);
        return mv;
    }

    @RequestMapping("/delete")
    public ModelAndView deleteRole(String ruuid){
        List<String> list=JSONObject.parseArray(ruuid,String.class);
        boolean flag=true;
        boolean flag1=true;
        for (String i: list) {
            Map<String,Object> map=new HashMap<>();
            Role role=new Role();
            role.setRuuid(i);
            map.put("role",role);
            flag1= roleService.delRolePermission(map);
            flag=roleService.deleteRole(role);
            if(!flag || !flag1){
                break;
            }
        }
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("flag",flag);
        return mv;
    }

    @RequestMapping("/getRoleById")
    public ModelAndView getRoleById(Role role){
        role=  roleService.getRoleById(role);
        ModelAndView mv=new ModelAndView();
        List<Permission> list=role.getPermissionSet();
        mv.addObject("permission",list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/update")
    public ModelAndView updateRole(Role role){
        boolean flag=roleService.updateRole(role);
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("flag",flag);
        return mv;
    }

    @RequestMapping("/add")
    public ModelAndView addRole(String rname,String pid){
        String ruuid=UUIDGenerator.getUUID();
        Role role=new Role();
        role.setRuuid(ruuid);
        role.setRname(rname);
        boolean flag=roleService.addRole(role);//将角色插入角色表
        Permission per=new Permission();
        List<Integer> list=JSONObject.parseArray(pid,Integer.class);
        boolean flag1=true;
        for (Integer in:list) {
            per.setPid(in);
            Map<String,Object> map=new HashMap<>();
            map.put("role",role);
            map.put("permission",per);
            flag1=roleService.insertRolePermission(map);//将权限角色id插入中间表
            if(!flag1) {
                break;
            }
        }
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        if (flag&&flag1){
            mv.addObject("flag",flag);
        }else {
            mv.addObject("flag",false);
        }
        return mv;
    }
    @RequestMapping("/allRole")
    public ModelAndView getAllRole(){
        ModelAndView mv=new ModelAndView();
        List<Role> list= roleService.searchAllRole();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("role",list);
        return mv;
    }
    @RequestMapping("/getPermission")
    public ModelAndView getPermission(){
        ModelAndView mv=new ModelAndView();
        List<Permission> list=  roleService.getPermissionById();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("permission",list);
        return mv;
    }


    @RequestMapping("/addRolePermission")
    public ModelAndView addRolePermission(String ruuid ,String pid){
        List<Integer> list = JSONObject.parseArray(pid,Integer.class);
        Role role=new Role();
        role.setRuuid(ruuid);
        Permission permission=new Permission();
        boolean flag=true;
        List<Permission> plist= roleService.getRoleById(role).getPermissionSet();
        List<Integer> ilist=new ArrayList<>();
        for (Permission p: plist) {
            ilist.add(p.getPid());
        }
        for (Integer inter:list) {
            if(!ilist.contains(inter)){
                permission.setPid(inter);
                Map<String,Object> map=new HashMap<>();
                map.put("role",role);
                map.put("permission",permission);
                flag=roleService.insertRolePermission(map);
                if (!flag){
                    break;
                }
            }
        }
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("flag",flag);
        return mv;
    }

    @RequestMapping("/delRolePermission")
    public ModelAndView delRolePermission(String ruuid ,String pid){
        List<Integer> list = JSONObject.parseArray(pid,Integer.class);
        Role role=new Role();
        role.setRuuid(ruuid);
        Permission permission=new Permission();
        boolean flag=true;
        List<Permission> plist= roleService.getRoleById(role).getPermissionSet();
        List<Integer> ilist=new ArrayList<>();
        for (Permission p: plist) {
            ilist.add(p.getPid());
        }
        for (Integer i:ilist) {  //判断当前台传的值不包含在原有权限中时的值进行删除
            if(!list.contains(i)){
                permission.setPid(i);
                Map<String,Object> map=new HashMap<>();
                map.put("role",role);
                map.put("permission",permission);
                flag=roleService.delRolePermission(map);
                if (!flag){
                    break;
                }
            }

        }
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("flag",flag);
        return mv;
    }




    public RoleService getRoleService() {
        return roleService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }
}
